package com.nowcoder.community.Utils;

/**
 * 生成Redis Key的工具类
 *
 * @author panxingyu
 */
public class RedisKeyUtil {
    /**
     * 连接符
     */
    public static final String SPLIT = ":";
    /**
     * 不同实体的点赞前缀（生成key）
     */
    public static final String PREFIX_ENTITY_LIKE = "like:entity";
    /**
     * 以用户为实体的点赞前缀
     */
    public static final String PREFIX_USER_LIKE = "like:user";
    /**
     * 关注实体前缀
     */
    public static final String PREFIX_FOLLOWEE = "followee";
    /**
     * 粉丝前缀
     */
    public static final String PREFIX_FOLLOWER = "follower";
    /**
     * 验证码前缀
     */
    public static final String PREFIX_KAPTCHA = "kaptcha";
    /**
     * 登录凭证前缀
     */
    public static final String PREFIX_TICKET = "ticket";
    /**
     * 用户前缀
     */
    public static final String PREFIX_USER = "user";
    /**
     * UV 前缀
     */
    public static final String PREFIX_UV = "uv";
    /**
     * DAU 前缀
     */
    public static final String PREFIX_DAU = "dau";
    /**
     * 帖子 前缀
     */
    public static final String PREFIX_POST = "post";

    /**
     * 用户实体的key
     */
    public static String getUserLikeKey(int userId) {
        return PREFIX_USER_LIKE + SPLIT + userId;
    }

    /**
     * 某个实体的key
     *
     * @param entityType 实体类型
     * @param entityId   实体id
     * @return Key
     */
    public static String getEntityLikeKey(int entityType, int entityId) {
        return PREFIX_ENTITY_LIKE + SPLIT + entityType + SPLIT + entityId;
    }

    /**
     * 某个用户关注的实体
     *
     * @param userId     用户ID
     * @param entityType 实体类型
     * @return followee:userId:entityType  对应的value --> zSet(entityId, now)
     */
    public static String getFolloweeKey(int userId, int entityType) {
        return PREFIX_FOLLOWEE + SPLIT + userId + SPLIT + entityType;
    }

    /**
     * 某个实体拥有的粉丝
     *
     * @param entityType 实体类型
     * @param entityId   实体ID
     * @return follower:entityType:entityId 对应的value --> zSet(userId, now)
     */
    public static String getFollowerKey(int entityType, int entityId) {
        return PREFIX_FOLLOWER + SPLIT + entityType + SPLIT + entityId;
    }

    /**
     * 生成验证码的 Key
     *
     * @param owner 验证码的拥有者(UUID)
     */
    public static String getKaptchaKey(String owner) {
        return PREFIX_KAPTCHA + SPLIT + owner;
    }

    /**
     * 生成登录凭证的 Key
     *
     * @param ticket 登录凭证字符串（封装在LoginTicket对象中）
     * @return Key
     */
    public static String getTicketKey(String ticket) {
        return PREFIX_TICKET + SPLIT + ticket;
    }

    /**
     * 根据用户ID 生成用户的 key
     *
     * @param userId 用户ID
     * @return Key
     */
    public static String getUserKey(int userId) {
        return PREFIX_USER + SPLIT + userId;
    }

    /**
     * 单日访客 UV
     */
    public static String getUVKey(String date) {
        return PREFIX_UV + SPLIT + date;
    }

    /**
     * 区间访客 UV
     */
    public static String getUVKey(String started, String endDate) {
        return PREFIX_UV + SPLIT + started + SPLIT + endDate;
    }

    /**
     * 单日活跃用户 DAU
     */
    public static String getDAUKey(String date) {
        return PREFIX_DAU + SPLIT + date;
    }

    /**
     * 区间活跃用户
     */
    public static String getDAUKey(String startDate, String endDate) {
        return PREFIX_DAU + SPLIT + startDate + SPLIT + endDate;
    }

    /**
     * 统计帖子分数key
     */
    public static String getPostScoreKey() {
        return PREFIX_POST + SPLIT + "score";
    }
}
